int key = 3;

//1位数码管
int pins[]={4,5,6,7,8,9,10,11};
unsigned char pinsNum = sizeof(pins)/sizeof(int);

void setPinsMode(){
  unsigned char i;
  for(i = 0;i < pinsNum;i++){
    pinMode(pins[i], OUTPUT);
  }
}

void num_0(){
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
}

void num_1(){
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
}

void num_2(){
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
}

void num_3(){
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
}

void num_4(){
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
}

void num_5(){
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
}

void num_6(){
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
}

void num_7(){
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
}

void num_8(){
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
}

void num_9(){
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
}

void num_A(){
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
}

void num_B(){
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
}

void num_C(){
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
}

void num_D(){
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
}

void num_E(){
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
}

void num_F(){
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
}

void turnOnLight(){
    num_0();
  delay(100);
    num_1();
  delay(100);
    num_2();
  delay(100);
    num_3();
  delay(100);
    num_4();
  delay(100);
    num_5();
  delay(100);
    num_6();
  delay(100);
    num_7();
  delay(100);
    num_8();
  delay(100);
    num_9();
  delay(100);
    num_A();
  delay(100);
    num_B();
  delay(100);
    num_C();
  delay(100);  
    num_D();
  delay(100);  
    num_E();
  delay(100);
    num_F();
  delay(100);
}

void turnOnLightWithRandomNum(){
  int randomNum = int(random(0,16));

  switch(randomNum){
    case 0:
      num_0();
      break;
    case 1:
      num_1();
      break;
    case 2:
      num_2();
      break;
    case 3:
      num_3();
      break;
    case 4:
      num_4();
      break;
    case 5:
      num_5();
      break;
    case 6:
      num_6();
      break;
    case 7:
      num_7();
      break;
    case 8:
      num_8();
      break;
    case 9:
      num_9();
      break;
    case 10:
      num_A();
      break;
    case 11:
      num_B();
      break;
    case 12:
      num_C();
      break;
    case 13:
      num_D();
      break;
    case 14:
      num_E();
      break;
    case 15:
      num_F();
      break;
    default:
      num_0();
      break;
  }
}

void setup() {
  setPinsMode();
  
  pinMode(key,OUTPUT);

  unsigned char i;
  for(i = 0;i < pinsNum;i++){
    pinMode(pins[i], OUTPUT);
  }
}

void loop() {
  int i;//定义电压值;
  boolean isRandom = false;
  while(1){
    i = analogRead(0);
    if(i > 1000){
      digitalWrite(key, LOW);
      turnOnLight();
      isRandom = false;
    }else{
      digitalWrite(key, HIGH);
      if(!isRandom){
        isRandom = true;
        turnOnLightWithRandomNum();
      }
    }
  }
}
